<?php
class Product  extends AdminAction{
	function index(){
		template('product',$this->data);
	}
	
	function getlist(){
		$db=$this->getDB();
		$page = P('page');
		$rows = P('rows');
		$result = array();
		$t = $db->get_assoc("select count(*) total from product where lang = '".$this->lang."'");
		$result['total'] = $t['total'];
		if(empty($page)||empty($rows)){
			$r = $db->query_assoc("select id,pname,date_format(createtime,'%Y-%c-%d') createtime from product where lang = '".$this->lang."' order by createtime desc limit 0,10");
			$result['rows'] = $r;
		}else{
			$start = ($page-1)*$rows;
			$r = $db->query_assoc("select id,pname,date_format(createtime,'%Y-%c-%d') createtime from product where lang = '".$this->lang."' order by createtime desc limit $start,$rows ");
			$result['rows'] = $r;
		}
		echo json_encode($result);
	}
	
	function proddelete(){
		$db=$this->getDB();
		$id = P('id');
		
		$db->query("delete from product where id = $id ");
		$message = array();
		$message = setMessage(true,'删除成功！',$message);
		echo json_encode($message);
	}
	
	function productadd(){
		$this->data['uuid'] = uuid2();
		
		template('productadd',$this->data);
	}
	
	function productadddo(){
		$db=$this->getDB();
		$imgids = P('imgid');
		$content = P('content');
		$content = addslashes($content);
		$title = P('title');
		$isdef = P('isdef');
		$pid = P('uuid');
		
		$db->query("insert into product (pname,pdesc,lang,createtime) values ('$title','$content','$this->lang',now())");
		$nid = $db->insert_id();
		
		if(is_array($imgids)){
			foreach ($imgids as $imgid){
				if($isdef == $imgid){
					$db->query("update product_images set isdef=1,pid=$pid and id = $imgid");
				}else{
					$db->query("update product_images set isdef=0,pid=$pid where id = $imgid");	
				}
			}
			
			if(empty($isdef)||$isdef==""){
				$fmid = $imgids[0];
				$db->query("update product_images set isdef=1,pid=$pid where id = $fmid");	
			}
		}
		
		message('产品添加成功！', '/admin.php/product/index');
	}
	
	function productupdate(){
		
		$db=$this->getDB();
		$id = P('id');
		
		$product = $db->get_assoc("select * from product where id = '$id'");
		$this->data['product'] = $product;
		
		$procimgs = $db->query_assoc("select * from product_images where pid = '$id'");
		$this->data['procimgs'] = $procimgs;
		
		$imgids = "";
		if(sizeof($procimgs)>=1){
			$i = 1;
			foreach ($procimgs as $img){
				if($i == sizeof($procimgs)){
					$imgids .=$img['id'];
				}else{
					$imgids .=$img['id'].',';
				}
			}
		}
		$this->data['imgids'] = $imgids;
		
		template('productupdate',$this->data);
	}
	
	function productupdatedo(){
		$db=$this->getDB();
		$sid = P('id');
		$imgids = P('imgid');
		$title = P('title');
		$isdef = P('isdef');
		$content = P('content');
		$content = addslashes($content);

		if(is_array($imgids)){
			foreach ($imgids as $imgid){
				if($isdef == $imgid){
					$db->query("update product_images set pid = $sid,isdef=1 where id = $imgid");	
				}else{
					$db->query("update product_images set pid = $sid,isdef=0 where id = $imgid");	
				}
			}
		}

		$db->query("update product set pdesc = '$content',pname='$title'  where id  = $sid");
 		
		message('产品编辑成功！', '/admin.php/product/index');
		
	}
	
	function addprocimg(){
		$db=$this->getDB();
		$productid = P('productid');

		$file = $this->loadLib('file');
		$filename = $file->uploadfile('procimg','attach/product');

		if(!empty($productid)){
			$db->query("insert into product_images (imgurl,createtime,pid) values ('$filename',now(),'$productid')");
		}else{
			$db->query("insert into product_images (imgurl,createtime) values ('$filename',now())");
		}
		$insertid = $db->insert_id();

		$message = array();
		$message['insertid'] = $insertid;
		$message['location'] = $filename;
		$message = setMessage(true,'上传成功！',$message);
		echo json_encode($message);
	}

	function pimgdelete(){
		$db=$this->getDB();
		
		$imgid = P('imgid');
		$procimg = $db->get_assoc("select * from product_images where id = $imgid");
		
		$file = $this->loadLib('file');
		$file ->unlink($procimg['imgurl']);
		
		$db->query("delete from product_images where id = $imgid");
		
		$message = array();
		$message = setMessage(true,'文件删除成功！',$message);
		echo json_encode($message);
	}
	
	function pimglist(){
		$db=$this->getDB();
		$pid = P('uuid');
		$page = P('page');
		$rows = P('rows');
		$result = array();
		$t = $db->get_assoc("select count(*) total from product_images where pid = '$pid' ");
		$result['total'] = $t['total'];
		if(empty($page)||empty($rows)){
			$r = $db->query_assoc("select id,imgurl,date_format(createtime,'%Y-%c-%d') createtime,isdef from product_images  where pid = '$pid'  order by createtime desc limit 0,10");
//			echo "select id,imgurl,date_format(createtime,'%Y-%c-%d') createtime,isdef from product_images  where pid = '$pid'  order by createtime desc limit 0,10";
			$result['rows'] = $r;
		}else{
			$start = ($page-1)*$rows;
			$r = $db->query_assoc("select id,imgurl,date_format(createtime,'%Y-%c-%d') createtime,isdef from product_images  where pid = '$pid'  order by createtime desc limit $start,$rows ");
			$result['rows'] = $r;
		}
		echo json_encode($result);
		
	}
	

	
}